Conditional (computer programming)
part 2/14 Β· 46.9 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Ifβthen(βelse)
The ifβthen or ifβthenβelse construction is used in many programming languages. Although the syntax varies from language to language, the basic structure (in pseudocode form) looks like this:
If (Boolean condition) Then
(consequent)
Else
(alternative)
End If
For example:
If stock = 0 Then
message = order new stock
Else
message = there is stock
End If
In the example code above, the part represented by (Boolean condition) constitutes a conditional expression, having intrinsic value (e.g., it may be substituted by either of the values True or False) but having no intrinsic meaning. In contrast, the combination of this expression, the If and Then surrounding it, and the consequent that follows afterward constitute a conditional statement, having intrinsic meaning (e.g., expressing a coherent logical rule) but no intrinsic value.
When an interpreter finds an If, it expects a Boolean condition β for example, x > 0, which means "the variable x contains a number that is greater than zero" β and evaluates that condition. If the condition is true, the statements following the then are executed. Otherwise, the execution continues in the following branch β either in the else block (which is usually optional), or if there is no else branch, then after the end If.
After either branch has been executed, control returns to the point after the end If.
History and development
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ